﻿using Dos.ORM;
using Model;
using System;
using System.Collections.Generic;
using JFModel.Const;
using System.Text;

namespace JFLogic.Logic.User
{
    public class UserLogic
    {
        /// <summary>
        /// 根据用户名获取某个人员信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public TB_User_Info GetModelByUserName(string userName)
        {
            return DB.Context.From<TB_User_Info>()
                .Where(d => (d.UserName == userName))
                .First();
        }

        /// <summary>
        /// 登录接口
        /// </summary>
        /// <param name="phone">手机号码</param>
        /// <param name="pwd">md5加密之后的密码</param>
        /// <returns>如果成功，则为空，失败返回 对不起，用户名或密码错误</returns>
        public string Login(string phone, string pwd)
        {

            string rel = "";
            bool checkUserExists = DB.Context.Exists<TB_User_Info>
                (d => (d.UserName == phone && d.UserState == UserState.NORMAL));
            if (!checkUserExists)
            {
                rel = "对不起，用户名或密码错误";
            }
            else
            {
                bool checkPwd = DB.Context.Exists<TB_User_Info>
                    (d => (d.UserName == phone && d.UserPass == pwd && d.UserState == UserState.NORMAL));
                if (!checkPwd)
                {
                    rel = "对不起，用户名或密码错误";
                }
            }
            return rel;
        }

        /// <summary>
        /// 插入人员数据
        /// </summary>
        /// <param name="userInfo">要插入的人员数据</param>
        /// <returns>如果为空，则插入成功，否则返回 对不起，人员数据插入错误，请联系管理员</returns>
        public string Insert(TB_User_Info userInfo)
        {
            string str = "";
            int rel = DB.Context.Insert<TB_User_Info>(userInfo);
            if (rel <= 0)
            {
                str = "对不起，人员数据插入错误，请联系管理员";
            }
            return str;
        }

        /// <summary>
        /// 修改人员数据
        /// </summary>
        /// <param name="userInfo">要修改的人员数据</param>
        /// <returns>如果为空，则插入成功，否则返回 对不起，人员数据修改错误，请联系管理员</returns>
        public string Update(TB_User_Info userInfo)
        {
            string str = "";
            int rel = DB.Context.Update<TB_User_Info>(userInfo);
            if (rel <= 0)
            {
                str = "对不起，人员数据修改错误，请联系管理员";
            }
            return str;
        }

        /// <summary>
        /// 删除人员数据
        /// </summary>
        /// <param name="userId">要删除的人员ID</param>
        /// <param name="operateId">操作人ID</param>
        /// <returns>如果成功，为空，否则 对不起，人员数据删除错误，请联系管理员， 没有找到该人员数据</returns>
        public string Delete(int userId, int operateId)
        {
            string str = "";
            TB_User_Info userInfo = Get(userId);
            if (userInfo != null)
            {
                userInfo.UserState = UserState.DELETE;
                userInfo.LastOperTime = DateTime.Now;
                userInfo.LastOperId = operateId;
                int rel = DB.Context.Update<TB_User_Info>(userInfo);
                if (rel <= 0)
                {
                    str = "对不起，人员数据删除错误，请联系管理员";
                }
            }
            else
            {
                str = "没有找到该人员数据";
            }

            return str;
        }

        /// <summary>
        /// 根据用户ID获取用户实例
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>无数据返回null</returns>
        public TB_User_Info Get(int userId)
        {
            return DB.Context.From<TB_User_Info>()
                .Where(d => d.UserId == userId)
                .First();
        }

        /// <summary>
        /// 检测用户名是否有效
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool CheckUserName(string userName)
        {
            return DB.Context.Exists<TB_User_Info>(d => d.UserName == userName);
        }

        /// <summary>
        /// 检测用户名与ip是否对应
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="IP"></param>
        /// <returns></returns>
        public bool CheckIPExists(string userName, string IP)
        {
            var where = new Where<TB_User_Info>();
            where.And<TB_BindIP_Info>((a, b) => (a.UserName == userName && a.UserState == UserState.NORMAL && b.IPAddress == IP));
            int rel = DB.Context.From<TB_User_Info>()
                .InnerJoin<TB_BindIP_Info>((a, b) => a.UserId == b.UserId)
                .Where(where)
                .Count();
            if (rel > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
